#ifndef T_CUSTOMRECTITEM_H
#define T_CUSTOMRECTITEM_H
#include <QObject>
#include <QGraphicsRectItem>
#include <QGraphicsSceneHoverEvent>
#include <QGraphicsSceneMouseEvent>
#include "common.h"
#include "T_ModuleGroupRectItem.h"

class ElaWidget;

class T_CustomRectItem : public QObject, public QGraphicsRectItem
{
    Q_OBJECT
public:
    T_CustomRectItem(QObject *parent = nullptr);
    ~T_CustomRectItem();

signals:
    void flushBlockLayout();
    void flushMainLayout();
    void flushSignalBlockLayout();
    void deleteMainItem();

public:
    void SetItemInfo(cutomitem data);

    // 父子关系操作
    void addChild(QGraphicsRectItem* child);
    void removeChild(QGraphicsRectItem* child);
    void updateLayout();
    QString getItemName();
    cutomitem getItemData();
    qreal getMars(LayoutDirectionHV direction);
    qreal getOuterMars(LayoutDirectionHV direction);
    void setMars(qreal left,qreal top,qreal right,qreal bottom);
    void addOuterRect_paintLine(const QStringList &tmpList);
    void setIsText(bool isText = true);

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
    void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
    void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
    void paint(QPainter *painter,
              const QStyleOptionGraphicsItem *option,
              QWidget *widget) override;

private:
    T_CustomRectItem* createCopy() const;
    void setCursorShape();
    void calculateGroupSize();
    //改变主体边距
    void setMarSpacing(qreal space_left,qreal space_top,qreal space_right,qreal space_bottom);
    void setNewRectF(const QRectF &rectf);

    void cchildItemSpace();
    void deleteItems();    

    void calaOldRects();
    void calcZoomRects(const QRectF &newRect);

    void drawGuideLines(QPainter *painter);
    void drawTipLines(QPainter *painter);
    void drawText(QPainter *painter);

    void closeElaWidget();
    void closeElaWidget2();

    void onOkElaWidget(QStringList list, qreal hspacing, qreal vspacing, const QString &hLayout, const QString &vLayout);

    void setLayoutDirect(const QString &str,const QString &str2);

    void setPositionBasedOnDirection(qreal diff_h, qreal diff_v,T_CustomRectItem *item,T_ModuleGroupRectItem *groupItem,qreal hspacing,qreal vspacing);

    void addOuterRect();
    void onAddOuterRect(QStringList list,qreal topMar,qreal bottomMar,qreal leftMar,qreal rightMar);

public slots:
    void cchildItemSpaceSignal(const QStringList &list,double hSpace,double vSpace,const QString &hLayout,const QString &vLayout);

private:
    qreal m_mar_spacing_left = 30.0;
    qreal m_mar_spacing_top = 30.0;
    qreal m_mar_spacing_right = 30.0;
    qreal m_mar_spacing_bottom = 30.0;

    qreal m_mar_spacing_left_outer = 10.0;
    qreal m_mar_spacing_top_outer = 10.0;
    qreal m_mar_spacing_right_outer = 10.0;
    qreal m_mar_spacing_bottom_outer = 10.0;

    cutomitem m_item_data;

    ResizeDirection m_resizeDirection;

    bool m_resizing;
    QPointF m_lastMousePos;
    QPointF m_lastPos;

    //新增
    // 父子关系成员
    QList<QGraphicsRectItem*> m_children;
    // 添加以下成员变量
    QRectF m_originalContainerRect;
    QHash<QGraphicsRectItem*, QPointF> m_originalChildPositions;
    QHash<QGraphicsRectItem*, QRectF> m_originalChildRects;
    qreal m_originalSpacing = 0;
    bool m_isFirstResize = true;

    //复制用的变量
    bool m_isCopying = false;
    // 复制体
    T_CustomRectItem* m_copyItem = nullptr;
    // 鼠标按下时的位置
    QPointF m_mousePressPos;
    // 是否显示辅助线
    bool m_showGuideLines = false;

    //子项间隔窗口
    ElaWidget *m_elaWidget{nullptr};
    ElaWidget *m_elaWidget2{nullptr};
    QMap<QString,QGraphicsRectItem *>m_oldRects;
    QMap<QString,QGraphicsRectItem *>m_addRects;

    //对齐方式
    LayoutDirectionHV m_layoutDirection{Right_Top};

    //绘制辅助线
    bool m_is_tip{true};
    //绘制文本
    bool m_is_text{true};
};
#endif // T_CUSTOMRECTITEM_H
